/**
 * 
 */
package org.pub.app.view;

import java.util.ArrayList;
import java.util.List;

import org.pub.app.lunch.AnimationTestActivity;
import org.pub.lunch.mashup.google.Restaurant;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * @author Austin Cho
 *
 */
/**
 * @author Austin Joe
 *
 */
public class SelectAnimation extends View {
	private final String TAG = "SelectAnimation";
	private Restaurant selectItem;
	private List<Restaurant> items;
	private List<Restaurant> drawItems = new ArrayList<Restaurant>();
	private Paint paint = new Paint();
	
	
	
	private int centerItemIndex = 6;
	
	private Paint overlayPaint = new Paint();
	private final int DISPLAY_ITEM_COUNT;
	private int delayTime = 10;
	int startIndex = 0;	
	
	private long animationTime = 0;	
	private final int FONT_HEIGHT = 20;
	private final int TEXT_START_XPOS = 100;
	private final int TEXT_GAP_Y = 40;
	private final int CELL_HEIGHT = 80;
	private final int MOVE_VALUE = CELL_HEIGHT+30;
	private int MOVE_Y = CELL_HEIGHT+30;
	private int cycleCount = 0;
	private int defineCycleCount = 0;
	private int cursorIndex = 0;
	
	

	private boolean isAnimation = true;
	/**
	 * 최종 선택된 음식점..
	 */
	private int selectedIndex = 0;
	
	
	AnimationTestActivity activity;
	/**
	 * @param context
	 */
	public SelectAnimation( AnimationTestActivity activity , int screenHeight ) {
		super(activity);
		this.activity = activity;
//		Window window = context.getWindow();
		paint.setColor( Color.WHITE );
		paint.setTextSize( FONT_HEIGHT );
		paint.setAntiAlias( true );
		overlayPaint.setColor( Color.RED );
		overlayPaint.setAlpha( 60 );
		DISPLAY_ITEM_COUNT = (screenHeight/CELL_HEIGHT)+3;		
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public SelectAnimation(Context context, AttributeSet attrs) {
		super(context, attrs);
		DISPLAY_ITEM_COUNT = (854/CELL_HEIGHT)+3;
	}

	
	
	public void setItem( List<Restaurant> items , int speed ){
		this.items = items;
		int count = 0;
		int defaultY = 0;
		for( Restaurant res : items ){
			if( count < DISPLAY_ITEM_COUNT ){
				res.setY( defaultY + (count*CELL_HEIGHT) );
//				Log.w( TAG , "y :: " + res.getY() );
				drawItems.add( res );
				cursorIndex++;
			}else{
				res.setY( items.get(cursorIndex-1).getY()+CELL_HEIGHT );
			}
			count++;
		}
		
//		Random rand = new Random();
//		rand.setSeed( System.currentTimeMillis() );
//		selectedIndex = Math.abs( rand.nextInt() ) % items.size();
//		defineCycleCount = cycleCountCalculator();
//		selectItem = items.get( selectedIndex );
//		Log.w( TAG , selectItem.getPhone() );
		
		
//		animationTime = ( 5000+ (Math.abs( rand.nextInt() ) % 3000) ) + System.currentTimeMillis();
		animationTime =  5000+ ( speed ) + System.currentTimeMillis();
	}
	
		
//	private int cycleCountCalculator(){
//		int cycleCount = 100/items.size();
//		if( selectedIndex == 0 ){
//			cycleCount +=1;
//		}		
//		return cycleCount;
//	}
	
	
		
	private void checkOverScreen( Restaurant data ){
		if( data.getY() <= -(CELL_HEIGHT + 10) ){
//			if( data.getPhone().equals( selectItem.getPhone() ) )
//				Log.w( TAG , "EQUALS ::" + data.getPhone() );
			drawItems.remove( data );
			cursorIndex++;
			if( cursorIndex == items.size() ){
				cursorIndex = 0;
			}
			Restaurant res = items.get(cursorIndex);
//			res.setY( 880 - MOVE_Y );
			res.setY( drawItems.get( drawItems.size() - 1 ).getY() + CELL_HEIGHT );
			drawItems.add( res );
		}
	}
	
	private void calculatorCenterYGap(){
		
	}
	
	boolean isStopRoutine = false;
	Rect orgRect = new Rect(0 , 0 , 80 , 80 );
	Rect destRect = new Rect( 0 , 0 , 80 , 80 );
	
	Rect topRect = new Rect( 0 , 400 , 480 , 480);
	Rect bottomRect = new Rect(0 , 480 , 480 , 854 );
	int aniCount = 0;
	@Override
	public void onDraw( Canvas canvas ){		
		if( items == null || items.size() < 0 )
			return;
		long current = System.currentTimeMillis();
		long ret = current - animationTime;
		
		if( ret > 4000){
			
			//멈춰야됨...
			if( !isStopRoutine ){
				selectItem = drawItems.get( centerItemIndex + 1  );
				isStopRoutine = true;
			}
			int retY = selectItem.getY() - 400;
//			Log.w( TAG , "RET Y:" + retY );
//			Log.w( TAG , "TEL:" + selectItem.getPhone() );
			if( retY == 0 ){
				MOVE_Y = 0;
				isAnimation = false;
				int index = items.indexOf( selectItem );
				Log.w( TAG , "CALL ANIMATION END" );
				activity.timeUpdateHandle.sendEmptyMessage( index );
			}
			else{
				if( retY > 10 )
					MOVE_Y = 10;
				else
					MOVE_Y = retY;
			}
		}else if( ret < 4000 && ret > 3000 ){
			MOVE_Y = 10;
		}else if( ret < 3000 && ret > 2000 ){
			MOVE_Y = (MOVE_VALUE-(MOVE_VALUE/2) );
		}else if( ret < 2000 && ret > 1000 ){			
			MOVE_Y = (MOVE_VALUE-(MOVE_VALUE/3) );
		}
//		if( ++aniCount % 10 == 1 && defineCycleCount <= 0 ){
//			if( isStopRoutine ){
//				int gapY = selectItem.getY() - 480;
//				if( gapY > 10 ){
//					MOVE_Y = -10;
//				}else if( gapY == 0 ){
//					MOVE_Y = 0;
//				}else{
//					MOVE_Y -= gapY;
//				}
//				Log.w( TAG , "STOP drawItems " );
//			}else if( drawItems.get(centerItemIndex+1).getPhone().equals(  selectItem.getPhone() ) ){
//				isStopRoutine = true;
//				//멈춰야됨...
//				Log.w( TAG , "centerItemIndex " );
//			}else{
//				if( !drawItems.get(centerItemIndex+1).getPhone().equals( selectItem.getPhone() ) ){
//					if( MOVE_Y > 40 )
//						MOVE_Y-=10;
//				}
//				
//				if( MOVE_Y < 0 ){
//					MOVE_Y = 0;
//				}
//				
////				Log.w( TAG , "getPhone ::" + drawItems.get(centerItemIndex+1).getPhone() );
////				Log.w( TAG , "getPhone ::" + selectItem.getPhone() );
////				Log.w( TAG , "::::::::::::::::::::::" );
//			}
//		}
		
//		int count = 0;
		
//		Log.w( TAG ,"HEIGHT ::" + canvas.getHeight() );
//		for( Restaurant res : drawItems ){
		int len = drawItems.size();
		for( int i = 0; i < len ; i++){
			Restaurant res = drawItems.get(i);		
			if( res.getBitmap() != null ){
				destRect.left = 0;
				destRect.top = res.getY();
				destRect.right = 80;
				destRect.bottom = res.getY()+ 80;
				canvas.drawBitmap( res.getBitmap() , null , destRect , paint);
			}
			canvas.drawText( res.getName() , TEXT_START_XPOS  , res.getY() + FONT_HEIGHT , paint );
			canvas.drawText( res.getPhone() , TEXT_START_XPOS , res.getY() +  FONT_HEIGHT + TEXT_GAP_Y , paint );
			canvas.drawLine( 0 , res.getY() +80 , canvas.getWidth() , res.getY() + 80 , paint );
			res.setY( res.getY() - MOVE_Y ); 
			checkOverScreen(res);
		}	
		

		canvas.drawRect( topRect , overlayPaint);
		
		if( isAnimation )
			postInvalidate();
	}
}
